#include "InputHandler.h"
#include "OgreStringConverter.h"
#include "Simulation.h"

#include "Player.h"
#include "GUIManager.h"
 
InputHandler::InputHandler(Simulation *sim, unsigned long hWnd)
{
 
    OIS::ParamList pl;
    pl.insert(OIS::ParamList::value_type("WINDOW", Ogre::StringConverter::toString(hWnd)));
 
    m_hWnd = hWnd;
    m_ois = OIS::InputManager::createInputSystem( pl );
    mMouse = static_cast<OIS::Mouse *>(m_ois->createInputObject( OIS::OISMouse, true ));
	
	mKeyboard = static_cast<OIS::Keyboard *>(m_ois->createInputObject( OIS::OISKeyboard, true ));
	
    mMouse->setEventCallback(this);
    mKeyboard->setEventCallback(this);
 
    m_simulation = sim;
}
 
InputHandler::~InputHandler()
{
    if (mMouse)
        m_ois->destroyInputObject(mMouse);
    if (mKeyboard)
        m_ois->destroyInputObject(mKeyboard);
 
    OIS::InputManager::destroyInputSystem(m_ois);
}
 
void InputHandler::capture()
{
    mMouse->capture();
    mKeyboard->capture();
}
 
void  InputHandler::setWindowExtents(int width, int height)
{
    //Set Mouse Region.. if window resizes, we should alter this to reflect as well
    const OIS::MouseState &ms = mMouse->getMouseState();
    ms.width = width;
    ms.height = height;
}
 
// MouseListener
bool InputHandler::mouseMoved(const OIS::MouseEvent &evt)
{
	GUIManager::Instance().MouseEvent(evt, OIS::MB_Left, 2);
	return true;
}
 
bool InputHandler::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID btn)
{
	Player::Instance().InjectMouse(btn, evt.state.buttonDown(btn));
	GUIManager::Instance().MouseEvent(evt, btn, 1);

    return true;
}
 
bool InputHandler::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID btn)
{
	Player::Instance().InjectMouse(btn, evt.state.buttonDown(btn));
	GUIManager::Instance().MouseEvent(evt, btn, 0);
    return true;
}

// KeyListener
bool InputHandler::keyPressed(const OIS::KeyEvent &evt)
{
	GUIManager::Instance().KeyboardEvent(evt.key, evt.text, true);
    return true;
}
 
bool InputHandler::keyReleased(const OIS::KeyEvent &evt)
{
	GUIManager::Instance().KeyboardEvent(evt.key, evt.text, false);
    if (evt.key == OIS::KC_ESCAPE)
        m_simulation->requestStateChange(SIM_SHUTDOWN);
	else if(evt.key == OIS::KC_Q)
	{
		if(m_simulation->getCurrentState()==SIM_SIMULATION)
			m_simulation->requestStateChange(SIM_GUI);
		else
			m_simulation->requestStateChange(SIM_SIMULATION);

		//GUIManager::Instance().GetGUI()->setVisiblePointer(m_simulation->getCurrentState()!=SIM_SIMULATION);
		MyGUI::Gui::getInstance().setVisiblePointer(m_simulation->getCurrentState()!=SIM_SIMULATION);
	}
    return true;
}

// JoyStickListener
bool InputHandler::buttonPressed(const OIS::JoyStickEvent &evt, int index)
{
    return true;
}
 
bool InputHandler::buttonReleased(const OIS::JoyStickEvent &evt, int index)
{
    return true;
}
 
bool InputHandler::axisMoved(const OIS::JoyStickEvent &evt, int index)
{
    return true;
}
 
bool InputHandler::povMoved(const OIS::JoyStickEvent &evt, int index)
{
    return true;
}